package com.xcm.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.google.common.collect.Lists;
import com.xcm.constant.DateFormatConstant;
import com.xcm.constant.OperationLogConstant;
import com.xcm.constant.PageConstant;
import com.xcm.dao.DeviceModelMapper;
import com.xcm.exception.ServiceException;
import com.xcm.model.TbDeviceModel;
import com.xcm.model.TbOperationLog;
import com.xcm.model.dto.DeviceModelDTO;
import com.xcm.model.vo.DeviceModelListVO;
import com.xcm.page.PageInfo;
import com.xcm.service.DeviceModelService;
import com.xcm.service.OperationLogService;
import com.xcm.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 终端型号Service实现类
 */
@Service(value = "deviceModelService")
public class DeviceModelServiceImpl implements DeviceModelService {
	@Autowired
	private DeviceModelMapper deviceModelMapper;
	@Autowired
	private OperationLogService operationLogService;
	/**
	 * 获取终端型号列表
	 *
	 * @return List<DeviceModelDTO>
	 */
	@Override
	public List<DeviceModelDTO> listDeviceModels() {
		return deviceModelMapper.listDeviceModels();
	}

	/**
	 * 获取终端型号分页列表
	 *
	 * @param pageNum  当前页
	 * @param pageSize 每页显示多少条
	 * @return List<TbDeviceModel>
	 */
	@Override
	public PageInfo<DeviceModelListVO> listDeviceModelByPage(Integer pageNum, Integer pageSize) {
		pageNum = pageNum == null ? PageConstant.DEFAULT_PAGE_NUM : pageNum;
		pageSize = pageSize == null ? PageConstant.DEFAULT_PAGE_SIZE : pageSize;
		PageHelper.startPage(pageNum, pageSize);
		Page<TbDeviceModel> page = deviceModelMapper.listDeviceModelByPage();
		if (page == null) {
			return new PageInfo<>(pageNum, pageSize, 0, Lists.newArrayList());
		}
		//把Page转化为PageInfo
		PageInfo<DeviceModelListVO> pageInfo = new PageInfo<>();
		pageInfo.setPageNum(pageNum);
		pageInfo.setPageSize(pageSize);
		pageInfo.setTotal(page.getTotal());

		List<TbDeviceModel> resultList = page.getResult();
		//把返回结果集封装成List<DeviceModelListVO>，并把long类型时间戳转为字符串日期
		List<DeviceModelListVO> deviceModelVoList = Lists.newArrayList();
		for (TbDeviceModel deviceModel : resultList) {
			DeviceModelListVO vo = new DeviceModelListVO();
			vo.setDeviceModelId(deviceModel.getDeviceModelId());
			vo.setDeviceModel(deviceModel.getDeviceModel());
			Long updateTime = deviceModel.getUpdateTime();
			if (updateTime == null) {
				vo.setUpdateTime("");
			} else {
				String stringDate = DateUtils.longTimeStampToStringDate(updateTime, DateFormatConstant.LONG_DATE_FORMAT_STR);
				vo.setUpdateTime(stringDate);
			}
			deviceModelVoList.add(vo);
		}
		pageInfo.setList(deviceModelVoList);
		return pageInfo;
	}

	/**
	 * 保存终端型号
	 *
	 * @param deviceModel TbDeviceModel
	 */
	@Override
	@Transactional(readOnly = false, rollbackFor = Exception.class)
	public void saveDeviceModel(TbDeviceModel deviceModel) throws ServiceException {
		//校验型号是否存在
		String model = deviceModel.getDeviceModel();
		TbDeviceModel oldDeviceModel = getDeviceModelByModel(model);
		if (oldDeviceModel != null) {
			throw new ServiceException("终端型号已存在");
		}
		deviceModel.setUpdateTime(System.currentTimeMillis());
		deviceModelMapper.saveDeviceModel(deviceModel);
		//保存操作日志
		TbOperationLog operationLog = new TbOperationLog();
		operationLog.setOperationTypeId(OperationLogConstant.OPERATION_LOG_TYPE_SYSTEM);
		operationLog.setOperationContent("新增终端型号：" + model);
		operationLog.setIsSuccess(OperationLogConstant.OPERATION_LOG_SUCCESS);
		operationLog.setOperationTime(System.currentTimeMillis());
		// TODO: 2018/4/9 处理用户ID和IP地址
		//展示设置操作用户名、用户真实姓名、IP为空，用户保存在统一登录系统
		operationLog.setUserName("");
		operationLog.setRealName("");
		operationLog.setOperationIp("");
		operationLogService.saveOperationLog(operationLog);
	}

	/**
	 * 修改终端型号
	 *
	 * @param deviceModel TbDeviceModel
	 */
	@Override
	public void updateDeviceModel(TbDeviceModel deviceModel) throws ServiceException {
		//校验型号是否存在
		String model = deviceModel.getDeviceModel();
		TbDeviceModel oldDeviceModel = getDeviceModelById(deviceModel.getDeviceModelId());
		if (oldDeviceModel != null) {
			if (!model.equals(oldDeviceModel.getDeviceModel())) {
				throw new ServiceException("终端型号已存在");
			}
		}
		deviceModelMapper.updateDeviceModel(deviceModel);
		//保存操作日志
		TbOperationLog operationLog = new TbOperationLog();
		operationLog.setOperationTypeId(OperationLogConstant.OPERATION_LOG_TYPE_SYSTEM);
		operationLog.setOperationContent("修改id为：" + deviceModel.getDeviceModelId() + "的终端型号为：" + deviceModel.getDeviceModel());
		operationLog.setIsSuccess(OperationLogConstant.OPERATION_LOG_SUCCESS);
		operationLog.setOperationTime(System.currentTimeMillis());
		// TODO: 2018/4/9 处理用户ID和IP地址
		//展示设置操作用户名、用户真实姓名、IP为空，用户保存在统一登录系统
		operationLog.setUserName("");
		operationLog.setRealName("");
		operationLog.setOperationIp("");
		operationLogService.saveOperationLog(operationLog);
	}

	/**
	 * 通过ID删除终端型号
	 *
	 * @param deviceModelIds 终端型号ID集(如：“1,2,3,4”)
	 */
	@Override
	public void deleteDeviceModelBatch(String deviceModelIds) {
		String splitChar = ",";
		for (String deviceModelId : deviceModelIds.split(splitChar)) {
			deviceModelMapper.deleteDeviceModelBatch(deviceModelId);
		}
		//保存操作日志
		TbOperationLog operationLog = new TbOperationLog();
		operationLog.setOperationTypeId(OperationLogConstant.OPERATION_LOG_TYPE_SYSTEM);
		operationLog.setOperationContent("删除id为：" + deviceModelIds + "的终端型号");
		operationLog.setIsSuccess(OperationLogConstant.OPERATION_LOG_SUCCESS);
		operationLog.setOperationTime(System.currentTimeMillis());
		// TODO: 2018/4/9 处理用户ID和IP地址
		//展示设置操作用户名、用户真实姓名、IP为空，用户保存在统一登录系统
		operationLog.setUserName("");
		operationLog.setRealName("");
		operationLog.setOperationIp("");
		operationLogService.saveOperationLog(operationLog);
	}

	/**
	 * 通过型号获取终端型号
	 *
	 * @param model 型号
	 * @return TbDeviceModel
	 */
	@Override
	public TbDeviceModel getDeviceModelByModel(String model) {
		return deviceModelMapper.getDeviceModelByModel(model);
	}

	/**
	 * 通过ID获取终端型号
	 *
	 * @param deviceModelId 终端型号ID
	 * @return TbDeviceModel
	 */
	@Override
	public TbDeviceModel getDeviceModelById(Long deviceModelId) {
		return deviceModelMapper.getDeviceModelById(deviceModelId);
	}
}
